﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Text;
using T.ServiceComponent.Cluster.Client;
using T.ServiceComponent.Kernel.Service;
using T.ServiceComponent.Monitor.Client;

namespace T.FAS.Runtime.Base.Common
{
    public class FASContextHelper
    {
        private static readonly object syncObj = new object();
        private static FASContextHelper instance = null;
        private FASContextHelper()
        {
            InitConfig();
        }
        public static FASContextHelper GetInstance()
        {
            if (instance == null)
            {
                lock (syncObj)
                {
                    if (instance == null)
                        instance = new FASContextHelper();
                }
            }
            return instance;
        }
        public string HostID { get; private set; }
        public string HostName { get; private set; }
        public string ProcessName { get; private set; }
        public int ProcessID { get; private set; }
        public string RootCaseMonitorMetaData = "Host.RootException";
        public string HostType = "FAS";

        private void InitConfig()
        {
            HostID = GetInstanceIDByAppCluster();
            HostName = Dns.GetHostName();
            ProcessName = Process.GetCurrentProcess().ProcessName;
            ProcessID = Process.GetCurrentProcess().Id;
        }
        /// <summary>
        /// 初始化RpcContext信息，用于新的链路节点创建
        /// </summary>
        /// <param name="isNeedSub">是否需要子链路</param>
        public void InitRpcContext()
        {
            var requestID = RpcContext.Current.RequestID;
            if (string.IsNullOrEmpty(requestID))
            {
                string dcID = AppClusterLocalClient.GetInstance().GetCurrentDataCenterId();
                var reqID = MonitorClient.GetRequestId(dcID);
                RpcContext.Current.RequestID = requestID;
            }
            //rpcID
            string rpcID = RpcContext.Current.RpcID;
            if (string.IsNullOrEmpty(rpcID))
            {
                rpcID = "0";
                RpcContext.Current.SetRpcID(rpcID);
            }
            else
            {
                RpcContext.Current.Increase();
            }
        }
        public void InitRpcContextWithSub()
        {
            var requestID = RpcContext.Current.RequestID;
            
            string dcID = AppClusterLocalClient.GetInstance().GetCurrentDataCenterId();
            var reqID = MonitorClient.GetRequestId(dcID);
            RpcContext.Current.RequestID = requestID;
            //rpcID
            string rpcID = RpcContext.Current.RpcID;
            if (string.IsNullOrEmpty(rpcID))
            {
                rpcID = "0";
                RpcContext.Current.SetRpcID(rpcID);
            }
            else
            {
                RpcContext.Current.SetRpcID(rpcID);
                RpcContext.Current.Increase();
            }

        }

        private string GetInstanceIDByAppCluster()
        {
            return AppClusterQueryClient.Current.GetCurrentAppHostID();
        }
        
    }
}
